package com.special.ResideMenuDemo;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.special.ResideMenu.ResideMenu;
import com.special.ResideMenu.ResideMenuItem;

public class MenuActivity extends FragmentActivity implements View.OnClickListener{

    private ResideMenu resideMenu = null;
    private MenuActivity mContext = null;
    private ResideMenuItem itemHome = null;
    private ResideMenuItem itemTeacher = null;
    private ResideMenuItem itemProfile = null;
    private ResideMenuItem itemCalendar = null;
    private ResideMenuItem itemSettings = null;
    private boolean mIsExist = false;
    private Handler mHandleExist = null;
    

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mContext = this;
        setUpMenu();
        if( savedInstanceState == null )
            changeFragment(new HomeFragment());
        mHandleExist = new Handler(){
        	public void handleMessage(Message msg){
        		mIsExist = false;
        	}
        };
    }

    private void setUpMenu() {

        // attach to current activity;
        resideMenu = new ResideMenu(this);
        resideMenu.setBackground(R.drawable.menu_background);
        resideMenu.attachToActivity(this);
        resideMenu.setMenuListener(menuListener);
        //valid scale factor is between 0.0f and 1.0f. leftmenu'width is 150dip. 
        resideMenu.setScaleValue(0.7f);

        // create menu items;
        itemHome     = new ResideMenuItem(this, R.drawable.icon_home,     "学校首页");
        itemTeacher = new ResideMenuItem(this, R.drawable.icon_home, "教务网");
        itemProfile  = new ResideMenuItem(this, R.drawable.icon_profile,  "用户信息");
        itemCalendar = new ResideMenuItem(this, R.drawable.icon_calendar, "课程表");
        itemSettings = new ResideMenuItem(this, R.drawable.icon_settings, "设置");

        itemHome.setOnClickListener(this);
        itemTeacher.setOnClickListener(this);
        itemProfile.setOnClickListener(this);
        itemCalendar.setOnClickListener(this);
        itemSettings.setOnClickListener(this);

        resideMenu.addMenuItem(itemHome, ResideMenu.DIRECTION_LEFT);
        resideMenu.addMenuItem(itemTeacher, ResideMenu.DIRECTION_LEFT);
        resideMenu.addMenuItem(itemProfile, ResideMenu.DIRECTION_LEFT);
        resideMenu.addMenuItem(itemCalendar, ResideMenu.DIRECTION_RIGHT);
        resideMenu.addMenuItem(itemSettings, ResideMenu.DIRECTION_RIGHT);

        findViewById(R.id.title_bar_left_menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
            }
        });
        findViewById(R.id.title_bar_right_menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resideMenu.openMenu(ResideMenu.DIRECTION_RIGHT);
            }
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return resideMenu.dispatchTouchEvent(ev);
    }

    @Override
    public void onClick(View view) {

        if (view == itemHome){
        	 Intent intent = new Intent();        
        	 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             intent.setAction("android.intent.action.VIEW");    
             Uri content_url = Uri.parse("http://www.hyit.edu.cn");   
             intent.setData(content_url); 
            getApplicationContext().startActivity(intent);
        }else if(view == itemTeacher){
        	 Intent intent = new Intent();        
        	 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             intent.setAction("android.intent.action.VIEW");    
             Uri content_url = Uri.parse("http://jwch.hyit.edu.cn/");   
             intent.setData(content_url); 
            getApplicationContext().startActivity(intent);
        }else if (view == itemProfile){
            changeFragment(new ProfileFragment());
        }else if (view == itemCalendar){
            changeFragment(new CalendarFragment());
        }else if (view == itemSettings){
            changeFragment(new SettingsFragment());
        }

        resideMenu.closeMenu();
    }

    private ResideMenu.OnMenuListener menuListener = new ResideMenu.OnMenuListener() {
        @Override
        public void openMenu() {
        }

        @Override
        public void closeMenu() {
        }
    };

    private void changeFragment(Fragment targetFragment){
        resideMenu.clearIgnoredViewList();
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.main_fragment, targetFragment, "fragment")
                .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                .commit();
    }

    // What good method is to access resideMenu？
    public ResideMenu getResideMenu(){
        return resideMenu;
    }
    
    public boolean onKeyDown(int keyCode, KeyEvent keyEvent){
		if(keyCode == KeyEvent.KEYCODE_BACK){
			changeFragment(new HomeFragment());
			isExist();
		}		
		return true;
	}
    
    private void isExist(){
    	if(!mIsExist){
    		mIsExist = true;
    		Toast.makeText(mContext, "点击第二次退出", Toast.LENGTH_SHORT).show();
    		mHandleExist.sendEmptyMessageDelayed(0x000, 2000);
    	}else{
    		quitSystem();
    	}
    }
    
    private void quitSystem(){
		Builder dialog = new AlertDialog.Builder(mContext)
		.setMessage("确定要退出考勤系统？")
		.setPositiveButton("确定", new Dialog.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				mContext.finish();
			}
		}).setNegativeButton("取消", new Dialog.OnClickListener(){
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		dialog.show();
	}
	
}
